﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Brushes = System.Windows.Media.Brushes;
using Color = System.Windows.Media.Color;
using FontFamily = System.Drawing.FontFamily;
using Pen = System.Windows.Media.Pen;

namespace UI
{
    /// <summary>
    /// Interaction logic for Chart.xaml
    /// </summary>
    public partial class Chart
    {
        private ChartPanel parent;

        private Color gridLinesColor, datesColor;
        private Font datesFont, currentDateFont;
        private int horizontalIndent, verticalIndent, redLinePosition, horizontalLinesCount;

        public Chart(ChartPanel container)
        {
            this.InitializeComponent();

            this.parent = container;

            this.gridLinesColor = Colors.LightGray;
            this.datesColor = Colors.Gray;
            this.currentDateFont = new Font(new FontFamily("calibri"), 12, System.Drawing.FontStyle.Bold);
            this.datesFont = new Font(new FontFamily("calibri"), 12, System.Drawing.FontStyle.Bold);
            this.horizontalIndent = 42;
            this.verticalIndent = 30;
            this.redLinePosition = (int)this.Height / 2;
        }

        public int VerticalIndent
        {
            get
            {
                return this.verticalIndent;
            }
            set
            {
                this.verticalIndent = value;
                this.InvalidateVisual();
            }
        }
        
        public int HorizontalIndent
        {
            get
            {
                return this.horizontalIndent;
            }
            set
            {
                this.horizontalIndent = value;
                this.InvalidateVisual();
            }
        }

        #region public properties

        public Color GridLinesColor
        {
            get
            {
                return this.gridLinesColor;
            }
            set
            {
                this.gridLinesColor = value;
                this.InvalidateVisual();
            }
        }

        public Color DatesColor
        {
            get
            {
                return this.datesColor;
            }
            set
            {
                this.datesColor = value;
                this.InvalidateVisual();
            }
        }

        public Font DatesFont
        {
            get
            {
                return this.datesFont;
            }

            set
            {
                this.datesFont = value;
                this.InvalidateVisual();
            }
        }

        public Font CurrentDateFont
        {
            get
            {
                return this.currentDateFont;
            }

            set
            {
                this.currentDateFont = value;
                this.InvalidateVisual();
            }
        }

        #endregion

        #region drawing functions

        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect r = new Rect(this.RenderSize);
            drawingContext.DrawRectangle(null, new Pen(Brushes.Black, 1), r);

            this.horizontalLinesCount = (int)(this.Height) / (this.verticalIndent + 1);
            int verticalSpace = (int)this.Height - this.horizontalLinesCount * this.verticalIndent;
            this.verticalIndent = 30 + verticalSpace / this.horizontalLinesCount;

            this.DrawGrid(drawingContext);
            this.DrawCharts(drawingContext);
            this.DrawDates(drawingContext);
            this.DrawRedLine(drawingContext);

            //e.Graphics.DrawImage(bitmap, 0, 0);

            //GC.Collect(1);
        }

        private void DrawDates(DrawingContext drawingContext)
        {
            string date, time;
            int x, y = 0;

            //calculating time shift between horizontals

            var ticks = (this.parent.EndTime - this.parent.StartTime).TotalMilliseconds;
            //int intervalsCount = 0;
            //while (y < this.chartPanel.Height)
            //{
            //    intervalsCount++;
            //    y += this.VerticalIndent;
            //}

            //intervalsCount--;
            //intervalsCount--;

            int timeShift = (int)(ticks / (this.horizontalLinesCount - 2));

            // drawing dates
            y = 0;
            DateTime toDraw = this.parent.StartTime;
            //while (y < this.chartPanel.Height)
            //{
            //    if (y < this.redLinePosition - this.verticalIndent / 2 || y > this.redLinePosition + this.verticalIndent / 2)
            //    {
            //        date = toDraw.ToShortDateString();
            //        time = toDraw.ToLongTimeString();
            //        x = 0;
            //        graphics.DrawString(time, this.DatesFont, new SolidBrush(this.DatesColor), x, y);
            //        x = this.Width - (int)graphics.MeasureString(date, this.DatesFont).Width;
            //        graphics.DrawString(date, this.DatesFont, new SolidBrush(this.DatesColor), x, y);
            //        toDraw = toDraw.AddMilliseconds(timeShift);
            //    }

            //    y += this.VerticalIndent;
            //}
        }

        private void DrawCharts(DrawingContext drawingContext)
        {
            //int a;
            //for (int x = 0; x < this.chartPanel.Height; x++)
            //{
            //    a = x;
            //    double y1 = Math.Sin(x * Math.PI / 360);
            //    double y2 = Math.Sin( (x + 1) * Math.PI / 360);

            //    graphics.SmoothingMode = SmoothingMode.AntiAlias;

            //    graphics.DrawLine(Pens.Blue, (int)(y1 * (this.Width - 2) / 2) + this.Width / 2, x, (int)(y2 * (this.Width - 2) / 2) + this.Width / 2, x + 1);
            //}
        }

        private void DrawGrid(DrawingContext drawingContext)
        {
            //int x = 0, y = this.VerticalIndent;
            //// drawing horizontals
            //while (y < this.chartPanel.Height)
            //{
            //    graphics.DrawLine(new Pen(this.GridLinesColor), x, y, this.Width - x - 2, y);
            //    y += this.VerticalIndent;
            //}

            //// drawing verticals
            //while (x < this.Width)
            //{
            //    graphics.DrawLine(new Pen(this.GridLinesColor), x, 0, x, this.chartPanel.Height);
            //    x += this.HorizontalIndent;
            //}
        }

        private void DrawRedLine(DrawingContext drawingContext)
        {
            //string date, time;
            //date = this.CurrentTime.ToShortDateString();
            //time = this.CurrentTime.ToLongTimeString();
            //this.redLinePosition = this.chartPanel.Height / 2;
            //graphics.DrawLine(Pens.Red, 0, this.redLinePosition, this.Width, this.redLinePosition);
            //graphics.DrawString(time, this.CurrentDateFont, Brushes.Red, 0, this.redLinePosition);
            //int x = this.Width - (int) graphics.MeasureString(date, this.CurrentDateFont).Width;
            //graphics.DrawString(date, this.CurrentDateFont, Brushes.Red, x, this.redLinePosition);
        }

        #endregion

        private void ChartResize(object sender, EventArgs e)
        {
            
        }

        private void CanvasSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Render, new Action(delegate { InvalidateVisual(); }));
        }
    }
}
